package com.blackcet.wholeexecption.execption;

import com.blackcet.wholeexecption.enums.ErrorCode;
import com.blackcet.wholeexecption.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

/**
 * <p> 描述 ：自定义全局异常处理类
 * @author : blackcat
 * @date : 2021/2/23 11:08
 */
@Slf4j
@ControllerAdvice
public class WholeExceptionHandler {

    /**
     * 301-缺少请求参数
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Result handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        log.error("发生缺少请求参数异常！原因是：", e);
        return new Result(ErrorCode.NO_PARAMTER_ERROR);
    }

    /**
     * 403-不支持当前请求方法
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("不支持当前请求方法！原因是：", e);
        return new Result(ErrorCode.METHOD_NOT_ALLOWED);
    }

    /**
     * 输入输出异常
     */
    @ExceptionHandler(value = IOException.class)
    @ResponseBody
    public  Result IOException(IOException e){
        log.error("发生输入输出异常！原因是：",e);
        return Result.error(ErrorCode.OUTPUT_ERROR);
    }

    /**
     * 类型强制转换异常
     */
    @ExceptionHandler(value = ClassCastException.class)
    @ResponseBody
    public  Result ClassCastException(ClassCastException e){
        log.error("发生类型强制转换异常！原因是：",e);
        return Result.error(ErrorCode.CAST_ERROR);
    }

    /**
     * 操作数据库异常
     */
    @ExceptionHandler(value = SQLException.class)
    @ResponseBody
    public  Result SQLException(SQLException e){
        log.error("发生操作数据库异常！原因是：",e);
        return Result.error(ErrorCode.DATABASE_ERROR);
    }

    /**
     * 文件未找到异常
     */
    @ExceptionHandler(value = FileNotFoundException.class)
    @ResponseBody
    public  Result FileNotFoundException(FileNotFoundException e){
        log.error("发生文件未找到异常！原因是：",e);
        return Result.error(ErrorCode.FILE_NO_FOND_ERROR);
    }

    /**
     * 数组下标越界异常
     */
    @ExceptionHandler(value = ArrayIndexOutOfBoundsException.class)
    @ResponseBody
    public  Result ArrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException e){
        log.error("发生数组下标越界异常！原因是：",e);
        return Result.error(ErrorCode.INDEX_BOUND_ERROR);
    }

    /**
     * 文件已结束异常
     */
    @ExceptionHandler(value = EOFException.class)
    @ResponseBody
    public  Result EOFException(EOFException e){
        log.error("发生文件已结束异常！原因是：",e);
        return Result.error(ErrorCode.END_FILE_ERROR);
    }

    /**
     * 字符串转换为数字异常
     */
    @ExceptionHandler(value = NumberFormatException.class)
    @ResponseBody
    public  Result NumberFormatException(NumberFormatException e){
        log.error("发生字符串转换为数字异常！原因是：",e);
        return Result.error(ErrorCode.STRING_TO_NUMBER_ERROR);
    }

    /**
     * 方法未找到异常
     */
    @ExceptionHandler(value = NoSuchMethodException.class)
    @ResponseBody
    public  Result NoSuchMethodException(NoSuchMethodException e){
        log.error("发生方法未找到异常！原因是：{}",e.getMessage());
        return Result.error(ErrorCode.METHOD_NO_FOND_ERROR);
    }

    /**
     * 处理自定义的业务异常
     */
    @ExceptionHandler(value = BlackcatException.class)
    @ResponseBody
    public  Result bizExceptionHandler(BlackcatException e){
        log.error("发生业务异常！原因是：",e);
        return Result.error(e.getErrorCode(),e.getErrorMsg());
    }

    /**
     * 处理空指针的异常
     */
    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public Result exceptionHandler( NullPointerException e){
        log.error("发生空指针异常！原因是：",e);
        return Result.error(ErrorCode.NULL_ERROR);
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e){
        log.error("未知异常！原因是：",e);
        return Result.error(ErrorCode.INTERNAL_SERVER_ERROR);
    }

    /**
     * 算术异常类
     */
    /*@ExceptionHandler(value = ArithmeticExecption.class)
    @ResponseBody
    public  Result ArithmeticExecption(ArithmeticExecption e){
        log.error("发生算术异常类！原因是：",e);
        return Result.error(ErrorCode.ARITHMETIC_ERROR);
    }*/

    /**
     * 违背安全原则异常
     */
    /*@ExceptionHandler(value = SecturityException.class)
    @ResponseBody
    public  Result SecturityException(SecturityException e){
        log.error("发生违背安全原则异常！原因是：",e);
        return Result.error(ErrorCode.SAFETY_PRINCIPLE_ERROR);
    }*/

    /**
     * 数组负下标异常
     */
    /*@ExceptionHandler(value = NegativeArrayException.class)
    @ResponseBody
    public  Result NegativeArrayException(NegativeArrayException e){
        log.error("发生数组负下标异常！原因是：",e);
        return Result.error(ErrorCode.ARRAY_LOSS_INDEX_ERROR);
    }*/
}
